import vue from 'vue'
import VueRouter from 'vue-router'
const Cinema = () => import('views/cinema/Cinema.vue')
const Film = () => import('views/film/Film.vue')
const Center = () => import('views/center/Center.vue')
const Message = () => import('views/message/Message.vue')
const ComingSoon = () => import('views/film/childern/ComingSoon.vue')
const NowPlaying = () => import('views/film/childern/NowPlaying.vue')
const Detail = () => import('views/detail/Detail.vue')
const Search = () => import('views/cinema/Search.vue')
const CinemaDetail = () => import('views/cinema/CinemaDetail.vue')
const CinemaInfo = () => import('views/cinema/CinemaInfo.vue')
const Cities = () => import('views/cities/Cities.vue')
const Login = () => import('views/login/Login.vue')
//解决编程式路由跳转报红问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}
vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/films'
  },
  {
    path: '/films',
    component: Film,
    children: [
      {
        path: '',
        redirect: 'nowplaying'
      },
      {
        path: 'nowplaying',
        component: NowPlaying
      },
      {
        path: 'comingsoon',
        component: ComingSoon
      }
    ]
  },
  {
    path: '/cinema',
    component: Cinema
  },
  {
    path: '/center',
    component: Center
  },
  {
    path: '/message',
    component: Message
  },
  {
    path: '/films/:id',
    component: Detail
  },
  {
    path: '/cinema/search',
    component: Search
  },
  {
    path: '/cinema/:id',
    component: CinemaInfo
  },
  {
    path: '/cinema/:id/film',
    component: CinemaDetail
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/city',
    component: Cities,
    meta: {
      auth: true
    }
  }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

router.beforeEach((to, form, next) => {
  document.title = '卖座电影'
  if (to.path === '/message') {
    location.href = 'https://m.maizuo.com/mz-act/active/ElzMZU125260'
  }
  if (!to.meta.auth) {
    if (!localStorage.getItem('vuex')) {
      next('/city')
    }
  }
  next()
})
export default router
